package com.wmm.jhdriver;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteDbHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "database.db";

    public static final int DB_VERSION = 2;

    public static final String TABLE_STUDENT = "records";

    //创建 students 表的 sql 语句
    private static final String STUDENTS_CREATE_TABLE_SQL ="create table " + TABLE_STUDENT + "("
            + "type varchar(11) not null,"
            + "list text not null,"
            + "time text not null,"
            + "ydhao text ,"
            + "kehu text "
            + ");";

    public SQLiteDbHelper(Context context) {
        // 传递数据库名与版本号给父类
        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        // 在这里通过 db.execSQL 函数执行 SQL 语句创建所需要的表
        // 创建 students 表
        db.execSQL(STUDENTS_CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String sql1 = "ALTER TABLE "+TABLE_STUDENT+" ADD COLUMN ydhao text";
        String sql2 = "ALTER TABLE "+TABLE_STUDENT+" ADD COLUMN kehu text";
        db.execSQL(sql1);
        db.execSQL(sql2);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        if (!db.isReadOnly()) {
            // 启动外键
            db.execSQL("PRAGMA foreign_keys = 1;");

//            //或者这样写
//            String query = String.format("PRAGMA foreign_keys = %s", "ON");
//            db.execSQL(query);
        }
    }
}